# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=setuptools
pkgbase=mingw-w64-python2-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
pkgver=44.1.1
pkgrel=1
pkgdesc="Easily download, build, install, upgrade, and uninstall Python packages (mingw-w64)"
arch=('any')
license=('PSF')
url="https://pypi.python.org/pypi/setuptools"
depends=("${MINGW_PACKAGE_PREFIX}-python2")
install=${_realname}2-${CARCH}.install
source=(${_realname}-${pkgver}.tar.gz::https://github.com/pypa/setuptools/archive/v${pkgver}.tar.gz
        '0001-mingw-python-fix.patch'
        '0002-Allow-usr-bin-env-in-script.patch'
        '0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch'
        '0004-dont-execute-msvc.patch'
        '0005-execv-warning.patch')
sha256sums=('d8203b2425014dd7b15b95c69a41454a98c90e5383c6a23d518f61c2e3e86bfd'
            'd3bc778723e63bbd6e43ab3536a015c547c8c20a95c6679a952284a7a4822996'
            '7bb5617c69566f5fbf1a3ee29a08179e1b41bdeabbc2998bf67615e9aa000424'
            '0e556505cb70ff3a5df856e352d5e2b3cf1582c25d02fc00a2d935a28576b28c'
            '2a854e21e99d6724999e2abeff08451923e9940b4092eb2b638702da17abdb73'
            'f8db482721900dbbfd19f52b9042954c85b9e3a61a5e4b68a82aeafec8cba34a')

prepare() {
  cd "${srcdir}/setuptools-${pkgver}"

  export SETUPTOOLS_INSTALL_WINDOWS_SPECIFIC_FILES=1

  patch -p1 -i ${srcdir}/0001-mingw-python-fix.patch
  patch -p1 -i ${srcdir}/0002-Allow-usr-bin-env-in-script.patch
  patch -p1 -i ${srcdir}/0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch
  patch -p1 -i ${srcdir}/0004-dont-execute-msvc.patch
  patch -p1 -i ${srcdir}/0005-execv-warning.patch

  cd "${srcdir}"

  # Compile our own MSYS2-layout and /usr/bin/env capable {cli,gui}{-32,-64}.exe to replace the precompiled binaries.
  # .. when arm is ready, add it to this.
  PATH=/mingw32/bin:$PATH gcc -DGUI=0           -O -s -o setuptools-${pkgver}/setuptools/cli-32.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw32/bin:$PATH gcc -DGUI=1 -mwindows -O -s -o setuptools-${pkgver}/setuptools/gui-32.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw64/bin:$PATH gcc -DGUI=0           -O -s -o setuptools-${pkgver}/setuptools/cli-64.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw64/bin:$PATH gcc -DGUI=1 -mwindows -O -s -o setuptools-${pkgver}/setuptools/gui-64.exe setuptools-${pkgver}/launcher.c

  # Remove post-release tag since we are using stable tags
  sed -e '/tag_build = .post/d' \
      -e '/tag_date = 1/d' \
      -i setuptools-${pkgver}/setup.cfg

  cp -r setuptools-${pkgver} setuptools-python-${CARCH}

  cd "${srcdir}"/setuptools-python-${CARCH}
  sed -i -e "s|^#\!.*/usr/bin/env python|#!/usr/bin/env python2|" setuptools/command/easy_install.py
}

build() {
  cd "${srcdir}"/setuptools-python-${CARCH}

  ${MINGW_PREFIX}/bin/python2 bootstrap.py
  ${MINGW_PREFIX}/bin/python2 setup.py build
}

package() {
  local _mingw_prefix=$(cygpath -wm ${MINGW_PREFIX})

  cd "${srcdir}/setuptools-python-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  rm ${pkgdir}${MINGW_PREFIX}/bin/easy_install-script.py
  rm ${pkgdir}${MINGW_PREFIX}/bin/easy_install.exe*

  sed -e "s|${_mingw_prefix}/bin/|${MINGW_PREFIX}/bin/|g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-2.7-script.py
}
